package com.sea.supply.service.impl;

import com.codecrane.core.Constants;
import com.codecrane.core.power.entity.PowerUser;
import com.codecrane.core.web.BatchImportUtils;
import com.sea.finance.entity.ExchangeRate;
import com.sea.finance.service.ExchangeRateService;
import com.sea.product.entity.Channel;
import com.sea.product.entity.Product;
import com.sea.product.mapper.ChannelMapper;
import com.sea.product.service.ProductService;
import com.sea.supply.entity.ProductPrice;
import com.sea.supply.entity.ProductPriceRecord;
import com.sea.supply.entity.Supplier;
import com.sea.supply.mapper.ProductPriceMapper;
import com.sea.supply.mapper.SupplierMapper;
import com.sea.supply.service.ProductPriceRecordService;
import com.sea.supply.service.ProductPriceService;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.List;

@Service
public class ProductPriceServiceImpl implements ProductPriceService {

    @Autowired
    private ProductPriceMapper productPriceMapper;
    @Autowired
    private ChannelMapper channelMapper;
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductPriceRecordService productPriceRecordService;
    @Autowired
    private ExchangeRateService exchangeRateService;
    @Override
    public List<ProductPrice> findByCnd(ProductPrice productPrice) {
        return productPriceMapper.findByCnd(productPrice);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int save(ProductPrice productPrice) {
        //查找对应的汇率
        ExchangeRate rate = exchangeRateService.findByRateName("CNY", productPrice.getProductMoneyType());
        if(null==rate){
            throw new RuntimeException("暂未查询到该币种汇率信息！");
        }
        //计算不含税单价
        Double percent = productPrice.getProductPercent();
        if(percent.equals(0.00)){
            productPrice.setProductUnitPrice(productPrice.getProductPrice());
        }else {
            productPrice.setProductUnitPrice(productPrice.getProductPrice()/(1+productPrice.getProductPercent() / 100));
        }
        productPrice.setProductPriceCny(rate.getRatePercent()*productPrice.getProductPrice());
        productPrice.setProductUnitPriceCny(rate.getRatePercent()*productPrice.getProductUnitPrice());
        int res = productPriceMapper.insert(productPrice);
        if(res>0){
            ProductPriceRecord record=new ProductPriceRecord();
            record.setProductPriceId(productPrice.getId());
            record.setProductPrice(productPrice.getProductPrice());
            record.setProductRemark(productPrice.getProductRemark());
            productPriceRecordService.insert(record);
        }
        return res;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importSku(InputStream inputStream, String fileName,String userRealName) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream, fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<ProductPrice> list = BatchImportUtils.iteratorForProductPrice(sheet);
            if (null == list || list.size() == 0) {
                throw new Exception("请至少保存一条SKU信息！");
            } else {
                for (ProductPrice productPrice : list) {
                    String productSupplierName = productPrice.getProductSupplierName();
                    Supplier supplier = supplierMapper.findBySupplierName(productSupplierName);
                    if (null == supplier) {
                        throw new Exception("未查询到该供应商:" + productSupplierName);
                    }
                    productPrice.setProductSupplierId(supplier.getId());
                    String productChannelName = productPrice.getProductChannelName();
                    //查询渠道
                    Channel channel = channelMapper.findByChannelName(productPrice.getProductChannelName());
                    if (null == channel) {
                        throw new Exception("未查询到该渠道:" + productChannelName);
                    }
                    //查询SKU
                    Product pro = productService.findBySku(productPrice.getProductSku());
                    if (null == pro) {
                        throw new Exception("未查询到该SKU:" + productPrice.getProductSku());
                    }
                    productPrice.setProductChannelId(channel.getId());
                    productPrice.setProductMember(userRealName);
                    ProductPrice product = productPriceMapper.findBySupplierChannelSku(productSupplierName, productChannelName, productPrice.getProductSku(), productPrice.getProductMoneyType());
                    if(null==product){
                        save(productPrice);
                    }else{
                        productPrice.setId(product.getId());
                        usave(productPrice);
                    }
                }
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int usave(ProductPrice productPrice) {
        //查找对应的汇率
        ExchangeRate rate = exchangeRateService.findByRateName("CNY", productPrice.getProductMoneyType());
        if(null==rate){
            throw new RuntimeException("暂未查询到该币种汇率信息！");
        }
        ProductPriceRecord lastPrice = productPriceRecordService.findLastPrice(productPrice.getId());
        //计算不含税单价
        Double percent = productPrice.getProductPercent();
        if(percent.equals(0.00)){
            productPrice.setProductUnitPrice(productPrice.getProductPrice());
        }else {
            productPrice.setProductUnitPrice(productPrice.getProductPrice()/(1+productPrice.getProductPercent() / 100));
        }
        productPrice.setProductPriceCny(rate.getRatePercent()*productPrice.getProductPrice());
        productPrice.setProductUnitPriceCny(rate.getRatePercent()*productPrice.getProductUnitPrice());

        if(null==lastPrice){
            ProductPriceRecord record=new ProductPriceRecord();
            record.setProductPriceId(productPrice.getId());
            record.setProductPrice(productPrice.getProductPrice());
            record.setProductRemark(productPrice.getProductRemark());
            productPriceRecordService.insert(record);
        }else {
            if (lastPrice.getProductPrice().equals(productPrice.getProductPrice())) {
            } else {
                ProductPriceRecord record = new ProductPriceRecord();
                record.setProductPriceId(productPrice.getId());
                record.setProductPrice(productPrice.getProductPrice());
                record.setProductRemark(productPrice.getProductRemark());
                productPriceRecordService.insert(record);
            }
        }

        return productPriceMapper.update(productPrice);
    }

    @Override
    public int delete(Integer id) {
        return productPriceMapper.delete(id);
    }

    @Override
    public ProductPrice findBySupplierChannelSku(String productSupplierName, String productChannelName, String productSku,String productMoneyType) {
        return productPriceMapper.findBySupplierChannelSku(productSupplierName,productChannelName,productSku,productMoneyType);
    }

    @Override
    public ProductPrice findById(Integer id) {
        return productPriceMapper.findById(id);
    }

    @Override
    public List<ProductPrice> exportHistory(ProductPrice productPrice) {
        return productPriceMapper.exportHistory(productPrice);
    }

    @Override
    public List<ProductPrice> getLatestPrice(ProductPrice productPrice) {
        return productPriceMapper.getLatestPrice(productPrice);
    }

    @Override
    public ProductPrice findByMaxId(ProductPrice productPrice) {
        return productPriceMapper.findByMaxId(productPrice);
    }
}
